<?php

namespace iBrand\Component\Purchase\Repositories\Eloquent;

use iBrand\Component\Purchase\Models\Transfer;
use iBrand\Component\Purchase\Repositories\TransferRepository;
use Prettus\Repository\Eloquent\BaseRepository;

class TransferRepositoryEloquent extends BaseRepository implements TransferRepository
{
    public function model()
    {
        return Transfer::class;
    }

    public function getTransferPaginate($shop_id, $where = [], $time = [], $limit = 10)
    {
        return $this->scopeQuery(function ($query) use ($shop_id, $where, $time) {
            $query = $query->Where(function ($query) use ($shop_id, $where, $time) {
                $query = $query->where(function ($query) use ($shop_id) {
                    $query->where('out_shop_id', $shop_id)->orWhere('in_shop_id', $shop_id);
                });

                if (count($where)) {
                    foreach ($where as $key => $value) {
                        if (is_array($value)) {
                            list($operate, $va) = $value;
                            $query = $query->where($key, $operate, $va);
                        } else {
                            $query = $query->where($key, $value);
                        }
                    }
                }

                if (count($time)) {
                    foreach ($time as $key => $value) {
                        if (is_array($value)) {
                            list($operate, $va) = $value;
                            $query = $query->where($key, $operate, $va);
                        } else {
                            $query = $query->where($key, $value);
                        }
                    }
                }
            });

            return $query->orderBy('created_at', 'desc');
        })->paginate($limit);
    }
}